package com.wechat.service;

import com.wechat.dao.UserDao;
import com.wechat.entity.User;
import com.wechat.utils.UUIDUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;


/**
 *
 * @author cgc
 * @date 18/4/9
 */
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    /**
     *
     */
    public int deleteById(Integer id) {
        return userDao.deleteById(id);
    }

    /**
     *
     */
    public int save(User record) {
        return userDao.save(record);
    }

    /**
     *
     */
    @Transactional(readOnly = true,rollbackFor = Exception.class)
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    /**
     *
     */
    public int update(User record) {
        return userDao.update(record);
    }


    /**
     *
     */
    public int updateBalance(Integer userId, BigDecimal amount) {
        return userDao.updateBalance(userId,amount);
    }

    /**
     *
     */
    @Transactional(readOnly = true,rollbackFor = Exception.class)
    public User findByOpenId(String openId) {
        return userDao.findByOpenId(openId);
    }

    @Transactional(readOnly = true,rollbackFor = Exception.class)
    public User findByUserKey(String key) {
        return userDao.findByUserKey(key);
    }



    /**
     * @param nickname
     * @return
     */
    @Transactional(readOnly = true,rollbackFor = Exception.class)
    public List<User> list(String nickname) {
        return userDao.list(nickname);
    }



    /**
     * 设置用户登录唯一凭证
     * @return
     */
    public String setLoginCode(Integer userId){
        String uuid = UUIDUtils.getUUID();
        String key = DigestUtils.md5Hex(String.valueOf(System.currentTimeMillis())).toUpperCase();
        String result =  uuid + key;
//        redisService.set(uuid + key, String.valueOf(userId));
//        redisService.expire(uuid.toString(),86400);
        return result;
    }

    /**
     * 设置登录用户
     * @return
     */
    public String setLoginUser(User user){
        String uuid = UUIDUtils.getUUID().toUpperCase();
        String key = DigestUtils.md5Hex(String.valueOf(System.currentTimeMillis())).toUpperCase();
        String result =  uuid + key;
        User user1 = new User();
        user1.setId(user.getId());
        user1.setUserKey(result);
        this.update(user1);
        return result;
    }

    /**
     * 获取当前登录用户
     * @return
     */
    public User getLoginUser(String key){
        User user = this.findByUserKey(key);
        return user;
    }

//    public Integer getUserIdByUuid(String uuid) {
//        if (StringUtils.isBlank(uuid)) {
//            return null;
//        }
//        String result = redisService.get(uuid);
//        if (StringUtils.isNotBlank(result)) {
//            return Integer.valueOf(result);
//        }
//        return null;
//    }
}
